﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SSE657.BahMap.Business;
using SSE657.BahMap.Common;
using SSE.BahMap.UI.WebMVC.Models;
namespace SSE.BahMap.UI.WebMVC.Controllers
{
    public class MainController : Controller
    {
       private IBAHMapSession _session;

        public MainController()
        {
            _session = new BAHMapSession();
        }

        public MainController(IBAHMapSession session)
        {
            _session = session;
        }
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult RatesByGrade(PayGrade grade, String dependents)
        {
            Boolean dependent=false;
            if (dependents == "true")
            {
                dependent = true;
            }
            
            List<SearchVM> result = new List<SearchVM>();
            try
            {
                Dictionary<RateData, double> rates = _session.GetRatesPercentiles(grade, dependent);
                foreach (RateData key in rates.Keys)
                {
                    result.Add(new SearchVM
                    {
                        MHA = key.Area.MHA,
                        Rate = key.Rate,
                        Percentile = 100d * Math.Round(rates[key], 2)
                    });
                }

                return View(result);
            }
            catch
            {
                return View("Error");
            }
        }

        public ActionResult AllRatesByZip(int zipcode)
        {
            try
            {
                var rates = _session.GetAllRates(zipcode);
                var result = new AllRatesVM();

                // Iterate over each PayGrade
                foreach (PayGrade key in rates.Keys)
                {
                    var set = rates[key];

                    // True = has dependents
                    result.WithDependents.Add(key, set[true].Rate);

                    // False = no dependents
                    result.WithoutDependents.Add(key, set[false].Rate);
                }
                return View(result);
            }
            catch
            {
                return View("Error");
            }
        }

        public ActionResult AllRatesByMha(String mha)
        {
            try
            {
                var rates = _session.GetAllRates(mha);
                var result = new AllRatesVM();

                // Iterate over each PayGrade
                foreach (PayGrade key in rates.Keys)
                {
                    var set = rates[key];

                    // True = has dependents
                    result.WithDependents.Add(key, set[true].Rate);

                    // False = no dependents
                    result.WithoutDependents.Add(key, set[false].Rate);
                }
                return View(result);
            }
            catch
            {
                return View("Error");
            }
        }

        public ActionResult SchoolsByZip(int zipcode)
        {
            try
            {
                var rates = _session.GetSchools(zipcode);
                return View(rates);
            }
            catch
            {
                return View("Error");
            }
        }

        public ActionResult SchoolsbyMha(String mha)
        {
            try
            {
                var rates = _session.GetSchools(mha);
                return View(rates);
            }
            catch
            {
                return View("Error");
            }
        }
    }
}
